package com.ch.ms.dto;

import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.Objects;
import java.util.function.Consumer;

public interface MapleDto extends Serializable {


    default <T extends MapleDto> T from(Object o) {
        return JSONObject.parseObject(JSONObject.toJSONString(o), (Type) this.getClass());
    }

    default <T> T to(Class<T> t) {
        Objects.requireNonNull(t);
        return JSONObject.parseObject(JSONObject.toJSONString(this), t);
    }


    default <T> T to(Class<T> tClass, Consumer<? super T> after) {
        Objects.requireNonNull(tClass);
        T t = to(tClass);
        Objects.requireNonNull(after);
        after.accept(t);
        return t;
    }
}
